<?php
/**
 + Created by:	Webit Solution
 * Company:		Webit Solution
 + Contact:		www.webitsolution.com , info@webitsolution.com
				www.wrecruit.com, developer@wrecruit.com
 * Created on:	Mar 21, 2012
 ^
 + Project: 		WRecruit 
 * File Name:	module/wrecruitstates.php
 ^ 
 * Description: Module for WRecruit
 ^ 
 * History:		1.0.0 - Mar 21, 2012
 ^ 
 */

defined('_JEXEC') or die('Restricted access');

jimport('joomla.plugin.plugin');
 
//The Content plugin Loadmodule
class plgContentWRecruitStates extends JPlugin
{   // for joomla 1.5
	public function onPrepareContent( &$row, &$params, $page=0 )  {
                if ( JString::strpos( $row->text, 'wrecruitstates' ) === false ) {
                        return true;
		}
              // expression to search for
                $regex = '/{wrecruitstates\s*.*?}/i';
                if ( !$this->params->get( 'enabled', 1 ) ) {
                        $row->text = preg_replace( $regex, '', $row->text );
                        return true;
                }
                preg_match_all( $regex, $row->text, $matches );
                $count = count( $matches[0] );
                if ( $count ) {
                        // Get plugin parameters
                        $style = $this->params->def( 'style', -2 );
                        $this->_process( $row, $matches, $count, $regex, $style );
                }
	}  // for  joomla 1.6
       public function onContentPrepare( $context, &$row, &$params, $page=0 ){
                if ( JString::strpos( $row->text, 'wrecruitstates' ) === false ) {
                        return true;
		}
              // expression to search for
                $regex = '/{wrecruitstates\s*.*?}/i';
                if ( !$this->params->get( 'enabled', 1 ) ) {
                        $row->text = preg_replace( $regex, '', $row->text );
                        return true;
                }
                preg_match_all( $regex, $row->text, $matches );
                $count = count( $matches[0] );
                if ( $count ) {
                        // Get plugin parameters
                        $style = $this->params->def( 'style', -2 );
                        $this->_process( $row, $matches, $count, $regex, $style );
                }
	}	
        protected function _process( &$row, &$matches, $count, $regex, $style )  {
                for ( $i=0; $i < $count; $i++ )
                {
                        $load = str_replace( 'wrecruitstates', '', $matches[0][$i] );
                        $load = str_replace( '{', '', $load );
                        $load = str_replace( '}', '', $load );
                        $load = trim( $load );
 
                        $modules       = $this->_load( $load, $style );
                        $row->text         = preg_replace( '{'. $matches[0][$i] .'}', $modules, $row->text );
                }
                $row->text = preg_replace( $regex, '', $row->text );
        }
        protected function _load( $position, $style=-2 ) {
                $document      = &JFactory::getDocument();
                $renderer      = $document->loadRenderer('module');
                $params                = array('style'=>$style);
		$noofjobs = $this->params->get('noofjobs', 7);
		$jobsinstate = $this->params->get('jobsinstate', 1);
		$allstates = $this->params->get('allstates', 0);
		$defaultcountry = $this->params->get('defaultcountry', 0);
		$theme = $this->params->get('theme', 1);
		$shtitle = $this->params->get('shtitle');
		$title = $this->params->get('title');
		$separator = $this->params->get('separator', 1);
		$colperrow = $this->params->get('colperrow',3);
		//sce
		$sliding= $this->params->get('sliding','1');
		$consecutivesliding= $this->params->get('consecutivesliding','3');
		$slidingdirection= $this->params->get('slidingdirection','1'); // 0 = left  , 1=up
		//sce


		$colwidth = Round(100/$colperrow,1);
		$colwidth = $colwidth.'%';

		//scs				
		if($this->params->get('Itemid')) $itemid = $this->params->get('Itemid');			
		else  $itemid =  JRequest::getVar('Itemid');
		//sce
		/** scs */
		$componentAdminPath = JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_wrecruit';
		$componentPath =  'components' . DS . 'com_wrecruit';
		require_once $componentPath . DS . 'models' . DS . 'mpwrecruit.php';
		$config = array( 'table_path' => $componentAdminPath . DS . 'tables');
		$model = new WRecruitModelMpWRecruit($config);
		$result = $model->getJobsState($defaultcountry,$theme);
		$states = $result[0];
		$states2 = $result[1];
		$trclass = $result[2];
		/*
		$countries = $result[0];
		$trclass = $result[1];	
		$dateformat = $result[2];
		*/
		/** sce */
		/*
		$db =& JFactory::getDBO();

		$query = "SELECT * FROM ". $db->nameQuote('#__wrecruit_config')." 
		WHERE configname = 'theme' OR configname = 'date_format' OR configname = 'defaultcountry'  ";
		$db->setQuery($query);
		$configs = $db->loadObjectList();
		foreach($configs AS $config){
			if ($config->configname == 'theme')$css = $config->configvalue;
			if ($config->configname == 'date_format')$dateformat = $config->configvalue;
			if ($config->configname == 'defaultcountry')$default_country = $config->configvalue;
		}	
		if ($theme == 1){ // wrecruit theme
			$trclass = array("odd", "even");
			if ($css == 'templatetheme.css') $trclass = array("sectiontableentry1", "sectiontableentry2");
			$document =& JFactory::getDocument();
			$document->addStyleSheet('components/com_wrecruit/css/'.$css);
		}elseif ($theme == 2){ // template theme
			$trclass = array("sectiontableentry1", "sectiontableentry2");
		}else 	$trclass = array("", ""); //no theme

		$curdate = date('Y-m-d H:i:s');
		$inquery =  " (SELECT COUNT(job.id) from ".$db->nameQuote('#__wrecruit_jobs')."  AS job WHERE state.code = job.state AND job.status = 1 AND job.startpublishing <= " . $db->Quote($curdate) . " AND job.stoppublishing >= " . $db->Quote($curdate). " ) as jobsbystate";                                
		$query =  "SELECT  DISTINCT state.id, state.name,state.code, state.countrycode, ";
		$query .= $inquery;
		$query .=  " FROM ".$db->nameQuote('#__wrecruit_states')." AS state 
					LEFT JOIN ".$db->nameQuote('#__wrecruit_jobs')."  AS job ON state.code = job.state                                                                                                                                                                                                                                                                                                                                                                                    
					WHERE state.enabled = ".$db->Quote('Y') ;
		if ($defaultcountry) $query .= " AND state.countrycode = ".$db->quote($default_country);
		$query .=  " ORDER BY state.name ";
		//echo $query;

		$query2 = "SELECT job.state, job.country, count(job.id) AS jobsbystate FROM ".$db->nameQuote('#__wrecruit_jobs')."  AS job WHERE job.status = 1 AND job.startpublishing <= " . $db->Quote($curdate) . " AND job.stoppublishing >= " . $db->Quote($curdate) . " AND job.state != '' " ;                           
		if ($defaultcountry) $query2 .= " AND job.country = ".$db->quote($default_country);
		$query2 .= " AND NOT EXISTS ( SELECT id FROM ".$db->nameQuote('#__wrecruit_states')." WHERE code = job.state) ";
		$query2 .= " GROUP BY job.state";
		//echo $query2;

		$db->setQuery($query);
		*/

		$contents = '<table cellpadding="0" cellspacing="0" border="0" width="100%" class="contentpane">';
			$isodd = 1;
			$count = 1;
			if ($shtitle == 1){
				$contents .=  '<tr class="'.$trclass[$isodd].'">';
				$contents .=  '<td colspan="'.$colperrow.'">';
				$contents .=  '<h2><u>'.$title.'</u></h2>';	
				if ($separator == 1) $contents .= '<hr style="border:dashed #C0C0C0; border-width:1px 0 0 0; height:0;line-height:0px;font-size:0;margin:0;padding:0;">';
				$contents .= '</td>';
				$contents .= '</tr>';
			}	
			if ($states ) { 
				foreach ($states as $state) {
					if ($allstates == 0){ 
						if($state->jobsbystate > 0 ) $printrecord = 1; else $printrecord = 0;
					}else $printrecord = 1;
					if ($noofjobs != -1){
						if ($count >= $noofjobs ) $printrecord = 0;
					}
					if ($printrecord == 1){	
						if ($count == 1){
							$isodd = 1 - $isodd;
							$contents .= '<tr class="'.$trclass[$isodd].'">';
						}

						$lnks = 'index.php?option=com_wrecruit&c=wrecruit&view=jobseeker&layout=listnewestjobs&country='.$state->countrycode.'&state='. $state->code .'&lt=1&Itemid='.$itemid; 
						$lnks = JRoute::_($lnks);
						$contents .= '<td width="'.$colwidth.'"><a href="'.$lnks.'" >'.$state->name;
									if ($jobsinstate == 1) $contents .=' ('. $state->jobsbystate.')';
								$contents .= '</a></td>';
					
						if ($count == $colperrow){
							$contents .= '</tr><tr class="'.$trclass[$isodd].'">';
							$count = 0;
						}
						$count++;			
					}	
				}
			}
			//$db->setQuery($query2);
			if ($states2) { 
				foreach ($states2 as $state) {
					if ($allstates == 0){ 
						if($state->jobsbystate > 0 ) $printrecord = 1; else $printrecord = 0;
					}else $printrecord = 1;
					if ($noofjobs != -1){
						if ($count >= $noofjobs ) $printrecord = 0;
					}
					if ($printrecord == 1){	
						$isodd = 1 - $isodd;
						$lnks = 'index.php?option=com_wrecruit&c=wrecruit&view=jobseeker&layout=listnewestjobs&country='.$state->country.'&state='. $state->state .'&lt=1&Itemid='.$itemid; 
						$lnks = JRoute::_($lnks);
						$contents .= '<td width="'.$colwidth.'"><a href="'.$lnks.'" >'.$state->state;
									if ($jobsinstate == 1) $contents .= ' ('. $state->jobsbystate.')';
								$contents .= '</a></td>';
					
						if ($count == $colperrow){
							$contents .= '</tr><tr class="'.$trclass[$isodd].'">';
							$count = 0;
						}
						$count++;			
					}	
				}
			}
			 if ($count-1 < $colperrow){
				for ($i = $count; $i <= $colperrow; $i++){
				    $contents .= '<td></td>';
				}
				$contents .= '</tr>';
			}

			$contents .= '</table>';
			
			//scs		
			if ($sliding == 1) {                    
				
				if($slidingdirection == 1 ){
					for ($a = 0; $a < $consecutivesliding; $a++){ 	$contents .= $contents; }
					$contents =  '<marquee  direction="up" scrollamount="2" onmouseover="this.stop();" onmouseout="this.start()";>'.$contents.'</marquee>';
					$contents = $contents.'<br clear="all">';
				}else{
					$tcontents = '<table cellpadding="0" cellspacing="0" border="1" width="100%" class="contentpane"> <tr>';
					for ($a = 0; $a < $consecutivesliding; $a++){ $scontents = '<td>'.$contents.'</td>'; }
					$contents = $tcontents.$scontents.'</tr></table>';
					$contents =  '<marquee  scrollamount="2" onmouseover="this.stop();" onmouseout="this.start()";>'.$contents.'</marquee>';
				}	
			}
			//sce
			
			return $contents;
	}	
	
}


?>

